from math import sqrt


class Mymath:
    def __init__(self, x, y):
        self.x = x
        self.y = y
#加

    def add(self):
        return self.x + self.y
#减

    def sub(self):
        return self.x - self.y
#乘

    def mul(self):
        return self.x * self.y
#除

    def dul(self):
        try:
            return self.x / self.y
        except Exception as e:
            print("错误信息如下：")
            print(e.args)
            return "除数不能为0！！"
#幂

    def power(self):
        return self.x ** self.y
#开方

    def math_sqrt(self):
        try:
            return sqrt(self.x)
        except Exception as e:
            print("错误信息如下：")
            print(e.args)
            return "只能对正数开方计算！！"




# 自由课题, 按照自己的想法, 将身边的事物抽象成类, 并创建多个实例.
class Car():
    def __init__(self, color, name, timet):
        self.color = color
        self.name = name
        self.timet = timet

    def introduction(self):
        return(f"The {self.name} car is {self.color}")


    def time1(self):
        return(f"The car has run {self.timet} years")



if __name__ == "__main__":
    c = Mymath(8, 4)
    print(c.add())
    print(c.sub())
    print(c.mul())
    print(c.dul())
    print(c.power())
    print("{:.2f}".format(c.math_sqrt()))
    print("-----------------------------------")
    my_car = Car('black', 'aodi', "8")
    print(my_car. introduction())
    print(my_car.time1())
    your_car = Car('red', 'BMS', "2")
    print(your_car.introduction())
    print(your_car.time1())